package com.feri.rbacsys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feri.rbacsys.dao.DepositoryDao;
import com.feri.rbacsys.entity.Storecost;
import com.feri.rbacsys.mapper.BuyorderMapper;
import com.feri.rbacsys.mapper.ReturnorderMapper;
import com.feri.rbacsys.service.StorecostService;
import com.feri.rbacsys.mapper.StorecostMapper;
import com.feri.rbacsys.vo.BuyAmounVo;
import com.feri.rbacsys.vo.R;
import com.feri.rbacsys.vo.ReturnAmounVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author 神经质的木子李
 * @description 针对表【t_storecost】的数据库操作Service实现
 * @createDate 2023-03-28 15:31:30
 */
@Service
public class StorecostServiceImpl extends ServiceImpl<StorecostMapper, Storecost>
        implements StorecostService {
    @Resource
    private StorecostMapper mapper;

    @Resource
    private DepositoryDao dao;

    @Resource
    private BuyorderMapper buyorderMapper;

    @Resource
    private ReturnorderMapper returnorderMapper;

    @Override
    public R addScost(Integer pid) {
        Storecost storecost = new Storecost();
        storecost.setPid(pid);
        storecost.setStorehouse("A区");
        storecost.setSku("箱");
        //库存数量
        int selectCount = dao.selectCount(pid);
        //购买数量
        BuyAmounVo buyAmounVo = buyorderMapper.getAmout(pid);
        int buyAmount = buyAmounVo.getBuyAmount();
        //退货数量
        ReturnAmounVo returnAmounVo = returnorderMapper.getAmout(pid);
        int returnAmount = returnAmounVo.getReturnAmount();
        int total = selectCount + buyAmount - returnAmount;
        storecost.setTotal(total);
        //总花费
        int cost = 50 * total;
        storecost.setCost(BigDecimal.valueOf(cost));
        if (mapper.insert(storecost) > 0) {
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R getall(int page, int size) {
        Page<Storecost> selectPage = mapper.selectPage(new Page<>(page, size), null);
        return R.ok(selectPage);
    }
}




